Imports System.Text


Namespace Core.Modules


    Public Class UserTranslator
        Inherits Translator


        Public Overrides Function Export() As Boolean
            Dim result As Boolean = True

            StatusMessage("Logging Into Web Store")
            Dim t As New BVweb.AuthenticationToken
            If WebServiceUtilities.Login(t) = False Then
                MsgBox("Login to store failed with username " & My.Settings.StoreUsername & ". Check your settings and try again.")
                Return False
            End If

            StatusMessage("Getting List of Users")
            Dim svc As New BVweb.WebServices3
            svc.Url = Helpers.FullServiceUrl
            Dim users() As BVweb.UserAccount = svc.Membership_UserAccount_FindAll(t)

            If users IsNot Nothing Then
                StatusMessage("Exporting Users")
                If users.Length < 1 Then
                    StatusMessage("No Users were Returned")
                    Return False
                End If
                RenderExport(users)
            Else
                MsgBox("No users were returned from the store. Unable to export.")
                Return False
            End If

            Return result
        End Function

        Public Overrides Function Import(ByVal lines() As String, ByVal cols() As MappedColumn) As Boolean
            Dim result = True

            Logging.WriteLine("Logging into store for Import...")
            StatusMessage("Logging Into Store for Import...")
            Dim t As New BVweb.AuthenticationToken
            If WebServiceUtilities.Login(t) = False Then
                Logging.WriteLine("Login to store failed with username " & My.Settings.StoreUsername & ". Check your settings and try again.")
                MsgBox("Login to store failed with username " & My.Settings.StoreUsername & ". Check your settings and try again.")
                Return False
            Else
                Logging.WriteLine("Successfully logged into store")
            End If
            Dim svc As New BVweb.WebServices3
            svc.Url = Helpers.FullServiceUrl

            Logging.WriteLine("Processing " & lines.Length & " lines")
            For i As Integer = 1 To lines.Length - 1                
                StatusMessage("Importing Item " & (i + 1))
                ImportItem(lines(i), cols, svc, t)
            Next

            Logging.WriteLine("Finished with Import!")
            StatusMessage("Finished with Import!")

            Return result
        End Function

        Private Sub ImportItem(ByVal l As String, ByVal cols() As MappedColumn, ByRef svc As BVweb.WebServices3, ByRef t As BVweb.AuthenticationToken)
            Dim data() As String = l.Split(vbTab)
            If data IsNot Nothing Then
                Try

                    Dim updateMode As Boolean = False
                    Dim bvin As String = data(0).Trim
                    If bvin = "new" Then
                        bvin = String.Empty
                    End If

                    Dim u As BVweb.UserAccount = svc.Membership_UserAccount_FindByBvin(t, bvin)
                    If u.Bvin = bvin Then
                        If bvin <> String.Empty Then
                            updateMode = True
                        End If
                    Else
                        u.Bvin = bvin
                    End If

                    For i As Integer = 0 To data.Length - 1
                        ParseField(data(i), cols(i).ColumnName, u)
                    Next

                    Dim result As Boolean = False

                    StatusMessage("Saving User " & u.UserName)
                    If updateMode Then
                        result = svc.Membership_UserAccount_Update(t, u)
                    Else
                        result = svc.Membership_UserAccount_Insert(t, u)
                    End If

                    If result Then
                        If updateMode Then
                            Logging.WriteLine("Updated User " & u.UserName)
                        Else
                            Logging.WriteLine("Created User " & u.UserName)
                        End If
                    Else
                        If updateMode Then
                            Logging.WriteLine("ERROR: Failed to update user " & u.UserName)
                        Else
                            Logging.WriteLine("ERROR: Failed to create user " & u.UserName)
                        End If
                    End If
                Catch ex As Exception
                    Logging.WriteLine("EXCEPTION: From Import Item: " & ex.Message & "|" & ex.StackTrace)
                End Try
            End If
        End Sub

        Private Sub ParseField(ByVal fieldData As String, ByVal columnName As String, ByRef u As BVweb.UserAccount)
            Select Case columnName
                Case "username"
                    u.UserName = MapperUtils.ParseFieldString(fieldData, u.UserName)
                Case "email"
                    u.Email = MapperUtils.ParseFieldString(fieldData, u.Email)
                Case "password"
                    u.Password = MapperUtils.ParseFieldString(fieldData, u.Password)
                Case "firstname"
                    u.FirstName = MapperUtils.ParseFieldString(fieldData, u.FirstName)
                Case "lastname"
                    u.LastName = MapperUtils.ParseFieldString(fieldData, u.LastName)
                Case "salt"
                    u.Salt = MapperUtils.ParseFieldString(fieldData, u.Salt)
                Case "comment"
                    u.Comment = MapperUtils.ParseFieldString(fieldData, u.Comment)
                Case "taxexempt"
                    u.TaxExempt = MapperUtils.ParseFieldBoolean(fieldData, u.TaxExempt)
                Case "pricinggroupid"
                    u.PricingGroupId = MapperUtils.ParseFieldString(fieldData, u.PricingGroupId)
                Case "customquestionanswers"
                    u.CustomQuestionAnswers = MapperUtils.ParseFieldString(fieldData, u.CustomQuestionAnswers)
                Case "failedlogincount"
                    u.FailedLoginCount = MapperUtils.ParseFieldInteger(fieldData, u.FailedLoginCount)
                Case "locked"
                    u.Locked = MapperUtils.ParseFieldBoolean(fieldData, u.Locked)
                Case "passwordformat"
                    u.PasswordFormat = ParseFieldPasswordFormat(fieldData, u.PasswordFormat)
                Case "passwordhint"
                    u.PasswordHint = MapperUtils.ParseFieldString(fieldData, u.PasswordHint)
                Case "passwordanswer"
                    u.PasswordAnswer = MapperUtils.ParseFieldString(fieldData, u.PasswordAnswer)
                Case "billingaddressnickname"
                    u.BillingAddress.NickName = MapperUtils.ParseFieldString(fieldData, u.BillingAddress.NickName)
                Case "billingaddressfirstname"
                    u.BillingAddress.FirstName = MapperUtils.ParseFieldString(fieldData, u.BillingAddress.FirstName)
                Case "billingaddressmiddleinitial"
                    u.BillingAddress.MiddleInitial = MapperUtils.ParseFieldString(fieldData, u.BillingAddress.MiddleInitial)
                Case "billingaddresslastname"
                    u.BillingAddress.LastName = MapperUtils.ParseFieldString(fieldData, u.BillingAddress.LastName)
                Case "billingaddresscompany"
                    u.BillingAddress.Company = MapperUtils.ParseFieldString(fieldData, u.BillingAddress.Company)
                Case "billingaddressline1"
                    u.BillingAddress.Line1 = MapperUtils.ParseFieldString(fieldData, u.BillingAddress.Line1)
                Case "billingaddressline2"
                    u.BillingAddress.Line2 = MapperUtils.ParseFieldString(fieldData, u.BillingAddress.Line2)
                Case "billingaddressline3"
                    u.BillingAddress.Line3 = MapperUtils.ParseFieldString(fieldData, u.BillingAddress.Line3)
                Case "billingaddresscity"
                    u.BillingAddress.City = MapperUtils.ParseFieldString(fieldData, u.BillingAddress.City)
                Case "billingaddressregionbvin"
                    u.BillingAddress.RegionBvin = MapperUtils.ParseFieldString(fieldData, u.BillingAddress.RegionBvin)
                Case "billingaddressregionname"
                    u.BillingAddress.RegionName = MapperUtils.ParseFieldString(fieldData, u.BillingAddress.RegionName)
                Case "billingaddresspostalcode"
                    u.BillingAddress.PostalCode = MapperUtils.ParseFieldString(fieldData, u.BillingAddress.PostalCode)
                Case "billingaddresscountrybvin"
                    u.BillingAddress.CountryBvin = MapperUtils.ParseFieldString(fieldData, u.BillingAddress.CountryBvin)
                Case "billingaddresscountryname"
                    u.BillingAddress.CountryName = MapperUtils.ParseFieldString(fieldData, u.BillingAddress.CountryName)
                Case "billingaddresscountybvin"
                    u.BillingAddress.CountyBvin = MapperUtils.ParseFieldString(fieldData, u.BillingAddress.CountyBvin)
                Case "billingaddresscountyname"
                    u.BillingAddress.CountyName = MapperUtils.ParseFieldString(fieldData, u.BillingAddress.CountyName)
                Case "billingaddressphone"
                    u.BillingAddress.Phone = MapperUtils.ParseFieldString(fieldData, u.BillingAddress.Phone)
                Case "billingaddressfax"
                    u.BillingAddress.Fax = MapperUtils.ParseFieldString(fieldData, u.BillingAddress.Fax)
                Case "billingaddressresidential"
                    u.BillingAddress.Residential = MapperUtils.ParseFieldBoolean(fieldData, u.BillingAddress.Residential)
                Case "billingaddresswebsiteurl"
                    u.BillingAddress.WebSiteUrl = MapperUtils.ParseFieldString(fieldData, u.BillingAddress.WebSiteUrl)
                Case "shippingaddressnickname"
                    u.ShippingAddress.NickName = MapperUtils.ParseFieldString(fieldData, u.ShippingAddress.NickName)
                Case "shippingaddressfirstname"
                    u.ShippingAddress.FirstName = MapperUtils.ParseFieldString(fieldData, u.ShippingAddress.FirstName)
                Case "shippingaddressmiddleinitial"
                    u.ShippingAddress.MiddleInitial = MapperUtils.ParseFieldString(fieldData, u.ShippingAddress.MiddleInitial)
                Case "shippingaddresslastname"
                    u.ShippingAddress.LastName = MapperUtils.ParseFieldString(fieldData, u.ShippingAddress.LastName)
                Case "shippingaddresscompany"
                    u.ShippingAddress.Company = MapperUtils.ParseFieldString(fieldData, u.ShippingAddress.Company)
                Case "shippingaddressline1"
                    u.ShippingAddress.Line1 = MapperUtils.ParseFieldString(fieldData, u.ShippingAddress.Line1)
                Case "shippingaddressline2"
                    u.ShippingAddress.Line2 = MapperUtils.ParseFieldString(fieldData, u.ShippingAddress.Line2)
                Case "shippingaddressline3"
                    u.ShippingAddress.Line3 = MapperUtils.ParseFieldString(fieldData, u.ShippingAddress.Line3)
                Case "shippingaddresscity"
                    u.ShippingAddress.City = MapperUtils.ParseFieldString(fieldData, u.ShippingAddress.City)
                Case "shippingaddressregionbvin"
                    u.ShippingAddress.RegionBvin = MapperUtils.ParseFieldString(fieldData, u.ShippingAddress.RegionBvin)
                Case "shippingaddressregionname"
                    u.ShippingAddress.RegionName = MapperUtils.ParseFieldString(fieldData, u.ShippingAddress.RegionName)
                Case "shippingaddresspostalcode"
                    u.ShippingAddress.PostalCode = MapperUtils.ParseFieldString(fieldData, u.ShippingAddress.PostalCode)
                Case "shippingaddresscountrybvin"
                    u.ShippingAddress.CountryBvin = MapperUtils.ParseFieldString(fieldData, u.ShippingAddress.CountryBvin)
                Case "shippingaddresscountryname"
                    u.ShippingAddress.CountryName = MapperUtils.ParseFieldString(fieldData, u.ShippingAddress.CountryName)
                Case "shippingaddresscountybvin"
                    u.ShippingAddress.CountyBvin = MapperUtils.ParseFieldString(fieldData, u.ShippingAddress.CountyBvin)
                Case "shippingaddresscountyname"
                    u.ShippingAddress.CountyName = MapperUtils.ParseFieldString(fieldData, u.ShippingAddress.CountyName)
                Case "shippingaddressphone"
                    u.ShippingAddress.Phone = MapperUtils.ParseFieldString(fieldData, u.ShippingAddress.Phone)
                Case "shippingaddressfax"
                    u.ShippingAddress.Fax = MapperUtils.ParseFieldString(fieldData, u.ShippingAddress.Fax)
                Case "shippingaddressresidential"
                    u.ShippingAddress.Residential = MapperUtils.ParseFieldBoolean(fieldData, u.ShippingAddress.Residential)
                Case "shippingaddresswebsiteurl"
                    u.ShippingAddress.WebSiteUrl = MapperUtils.ParseFieldString(fieldData, u.ShippingAddress.WebSiteUrl)
            End Select
        End Sub

        Private Sub RenderExport(ByVal data() As BVweb.UserAccount)
            Dim sb As New StringBuilder

            WriteHeaders(sb)
            For i As Integer = 0 To data.Length - 1
                StatusMessage("Exporting User " & (i + 1) & " of " & data.Length)
                WriteItem(data(i), sb)
            Next

            Dim v As New ExportView
            v.OutputText = sb.ToString
            v.ShowDialog()
            v.Dispose()
        End Sub

        Private Sub WriteHeaders(ByRef sb As StringBuilder)
            StatusMessage("Writing Headers")

            sb.Append("Bvin")
            sb.Append(vbTab)
            sb.Append("UserName")
            sb.Append(vbTab)
            sb.Append("Email")
            sb.Append(vbTab)
            sb.Append("Password")
            sb.Append(vbTab)
            sb.Append("FirstName")
            sb.Append(vbTab)
            sb.Append("LastName")
            sb.Append(vbTab)
            sb.Append("Salt")
            sb.Append(vbTab)
            sb.Append("Comment")
            sb.Append(vbTab)
            sb.Append("TaxExempt")
            sb.Append(vbTab)
            sb.Append("PricingGroupId")
            sb.Append(vbTab)
            sb.Append("CustomQuestionAnswers")
            sb.Append(vbTab)
            sb.Append("FailedLoginCount")
            sb.Append(vbTab)
            sb.Append("Locked")
            sb.Append(vbTab)
            sb.Append("LockedUntil")
            sb.Append(vbTab)
            sb.Append("PasswordFormat")
            sb.Append(vbTab)
            sb.Append("PasswordHint")
            sb.Append(vbTab)
            sb.Append("PasswordAnswer")
            sb.Append(vbTab)
            sb.Append("BillingAddressNickName")
            sb.Append(vbTab)
            sb.Append("BillingAddressFirstName")
            sb.Append(vbTab)
            sb.Append("BillingAddressMiddleInitial")
            sb.Append(vbTab)
            sb.Append("BillingAddressLastName")
            sb.Append(vbTab)
            sb.Append("BillingAddressCompany")
            sb.Append(vbTab)
            sb.Append("BillingAddressLine1")
            sb.Append(vbTab)
            sb.Append("BillingAddressLine2")
            sb.Append(vbTab)
            sb.Append("BillingAddressLine3")
            sb.Append(vbTab)
            sb.Append("BillingAddressCity")
            sb.Append(vbTab)
            sb.Append("BillingAddressRegionBvin")
            sb.Append(vbTab)
            sb.Append("BillingAddressRegionName")
            sb.Append(vbTab)
            sb.Append("BillingAddressPostalCode")
            sb.Append(vbTab)
            sb.Append("BillingAddressCountryBvin")
            sb.Append(vbTab)
            sb.Append("BillingAddressCountryName")
            sb.Append(vbTab)
            sb.Append("BillingAddressCountyBvin")
            sb.Append(vbTab)
            sb.Append("BillingAddressCountyName")
            sb.Append(vbTab)
            sb.Append("BillingAddressPhone")
            sb.Append(vbTab)
            sb.Append("BillingAddressFax")
            sb.Append(vbTab)
            sb.Append("BillingAddressResidential")
            sb.Append(vbTab)
            sb.Append("BillingAddressWebSiteUrl")
            sb.Append(vbTab)
            sb.Append("ShippingAddressNickName")
            sb.Append(vbTab)
            sb.Append("ShippingAddressFirstName")
            sb.Append(vbTab)
            sb.Append("ShippingAddressMiddleInitial")
            sb.Append(vbTab)
            sb.Append("ShippingAddressLastName")
            sb.Append(vbTab)
            sb.Append("ShippingAddressCompany")
            sb.Append(vbTab)
            sb.Append("ShippingAddressLine1")
            sb.Append(vbTab)
            sb.Append("ShippingAddressLine2")
            sb.Append(vbTab)
            sb.Append("ShippingAddressLine3")
            sb.Append(vbTab)
            sb.Append("ShippingAddressCity")
            sb.Append(vbTab)
            sb.Append("ShippingAddressRegionBvin")
            sb.Append(vbTab)
            sb.Append("ShippingAddressRegionName")
            sb.Append(vbTab)
            sb.Append("ShippingAddressPostalCode")
            sb.Append(vbTab)
            sb.Append("ShippingAddressCountryBvin")
            sb.Append(vbTab)
            sb.Append("ShippingAddressCountryName")
            sb.Append(vbTab)
            sb.Append("ShippingAddressCountyBvin")
            sb.Append(vbTab)
            sb.Append("ShippingAddressCountyName")
            sb.Append(vbTab)
            sb.Append("ShippingAddressPhone")
            sb.Append(vbTab)
            sb.Append("ShippingAddressFax")
            sb.Append(vbTab)
            sb.Append("ShippingAddressResidential")
            sb.Append(vbTab)
            sb.Append("ShippingAddressWebSiteUrl")
            sb.Append(vbTab)
            sb.Append("CreationDate")
            sb.Append(vbTab)
            sb.Append("LastUpdated")
            sb.Append(System.Environment.NewLine)

        End Sub

        Private Sub WriteItem(ByVal u As BVweb.UserAccount, ByRef sb As StringBuilder)
            sb.Append(u.Bvin)
            sb.Append(vbTab)
            sb.Append(SafeString(u.UserName))
            sb.Append(vbTab)
            sb.Append(SafeString(u.Email))
            sb.Append(vbTab)
            sb.Append(SafeString(u.Password))
            sb.Append(vbTab)
            sb.Append(SafeString(u.FirstName))
            sb.Append(vbTab)
            sb.Append(SafeString(u.LastName))
            sb.Append(vbTab)
            sb.Append(SafeString(u.Salt))
            sb.Append(vbTab)
            sb.Append(SafeString(u.Comment))
            sb.Append(vbTab)
            sb.Append(SafeBool(u.TaxExempt))
            sb.Append(vbTab)
            sb.Append(SafeString(u.PricingGroupId))
            sb.Append(vbTab)
            sb.Append(SafeString(u.CustomQuestionAnswers))
            sb.Append(vbTab)
            sb.Append(SafeString(u.FailedLoginCount.ToString))
            sb.Append(vbTab)
            sb.Append(SafeBool(u.Locked))
            sb.Append(vbTab)
            sb.Append(SafeString(u.LockedUntil.ToString))
            sb.Append(vbTab)
            sb.Append(SafePasswordFormat(u.PasswordFormat))
            sb.Append(vbTab)
            sb.Append(SafeString(u.PasswordHint))
            sb.Append(vbTab)
            sb.Append(SafeString(u.PasswordAnswer))
            sb.Append(vbTab)
            sb.Append(SafeString(u.BillingAddress.NickName))
            sb.Append(vbTab)
            sb.Append(SafeString(u.BillingAddress.FirstName))
            sb.Append(vbTab)
            sb.Append(SafeString(u.BillingAddress.MiddleInitial))
            sb.Append(vbTab)
            sb.Append(SafeString(u.BillingAddress.LastName))
            sb.Append(vbTab)
            sb.Append(SafeString(u.BillingAddress.Company))
            sb.Append(vbTab)
            sb.Append(SafeString(u.BillingAddress.Line1))
            sb.Append(vbTab)
            sb.Append(SafeString(u.BillingAddress.Line2))
            sb.Append(vbTab)
            sb.Append(SafeString(u.BillingAddress.Line3))
            sb.Append(vbTab)
            sb.Append(SafeString(u.BillingAddress.City))
            sb.Append(vbTab)
            sb.Append(SafeString(u.BillingAddress.RegionBvin))
            sb.Append(vbTab)
            sb.Append(SafeString(u.BillingAddress.RegionName))
            sb.Append(vbTab)
            sb.Append(SafeString(u.BillingAddress.PostalCode))
            sb.Append(vbTab)
            sb.Append(SafeString(u.BillingAddress.CountryBvin))
            sb.Append(vbTab)
            sb.Append(SafeString(u.BillingAddress.CountryName))
            sb.Append(vbTab)
            sb.Append(SafeString(u.BillingAddress.CountyBvin))
            sb.Append(vbTab)
            sb.Append(SafeString(u.BillingAddress.CountyName))
            sb.Append(vbTab)
            sb.Append(SafeString(u.BillingAddress.Phone))
            sb.Append(vbTab)
            sb.Append(SafeString(u.BillingAddress.Fax))
            sb.Append(vbTab)
            sb.Append(SafeBool(u.BillingAddress.Residential))
            sb.Append(vbTab)
            sb.Append(SafeString(u.BillingAddress.WebSiteUrl))
            sb.Append(vbTab)
            sb.Append(SafeString(u.ShippingAddress.NickName))
            sb.Append(vbTab)
            sb.Append(SafeString(u.ShippingAddress.FirstName))
            sb.Append(vbTab)
            sb.Append(SafeString(u.ShippingAddress.MiddleInitial))
            sb.Append(vbTab)
            sb.Append(SafeString(u.ShippingAddress.LastName))
            sb.Append(vbTab)
            sb.Append(SafeString(u.ShippingAddress.Company))
            sb.Append(vbTab)
            sb.Append(SafeString(u.ShippingAddress.Line1))
            sb.Append(vbTab)
            sb.Append(SafeString(u.ShippingAddress.Line2))
            sb.Append(vbTab)
            sb.Append(SafeString(u.ShippingAddress.Line3))
            sb.Append(vbTab)
            sb.Append(SafeString(u.ShippingAddress.City))
            sb.Append(vbTab)
            sb.Append(SafeString(u.ShippingAddress.RegionBvin))
            sb.Append(vbTab)
            sb.Append(SafeString(u.ShippingAddress.RegionName))
            sb.Append(vbTab)
            sb.Append(SafeString(u.ShippingAddress.PostalCode))
            sb.Append(vbTab)
            sb.Append(SafeString(u.ShippingAddress.CountryBvin))
            sb.Append(vbTab)
            sb.Append(SafeString(u.ShippingAddress.CountryName))
            sb.Append(vbTab)
            sb.Append(SafeString(u.ShippingAddress.CountyBvin))
            sb.Append(vbTab)
            sb.Append(SafeString(u.ShippingAddress.CountyName))
            sb.Append(vbTab)
            sb.Append(SafeString(u.ShippingAddress.Phone))
            sb.Append(vbTab)
            sb.Append(SafeString(u.ShippingAddress.Fax))
            sb.Append(vbTab)
            sb.Append(SafeBool(u.ShippingAddress.Residential))
            sb.Append(vbTab)
            sb.Append(SafeString(u.ShippingAddress.WebSiteUrl))
            sb.Append(vbTab)
            sb.Append(SafeString(u.CreationDate.ToString))
            sb.Append(vbTab)
            sb.Append(SafeString(u.LastUpdated.ToString))
            sb.Append(System.Environment.NewLine)
        End Sub

        Private Function SafeString(ByVal input As String) As String
            Dim result As String = input.Replace(vbTab, "")
            Return result
        End Function

        Private Function SafeBool(ByVal input As Boolean) As String
            If input = True Then
                Return "True"
            Else
                Return "False"
            End If
        End Function

        Private Function SafePasswordFormat(ByVal s As BVweb.UserPasswordFormat) As String
            Dim result As String = "Hashed"
            Select Case s
                Case BVweb.UserPasswordFormat.ClearText
                    result = "ClearText"
                Case BVweb.UserPasswordFormat.Encrypted
                    result = "Encrypted"
                Case BVweb.UserPasswordFormat.Hashed
                    result = "Hashed"               
            End Select
            Return result
        End Function

        Private Function ParseFieldPasswordFormat(ByVal input As String, ByVal originalData As BVweb.UserPasswordFormat) As BVweb.ProductStatus
            Dim data As String = MapperUtils.ParseFieldString(input, SafePasswordFormat(originalData))
            Dim result As BVweb.UserPasswordFormat = originalData
            Select Case data.ToLower
                Case "cleartext", "c", "clear", "plain", "plaintext", "none"
                    result = BVweb.UserPasswordFormat.ClearText
                Case "hashed", "h", "hash"
                    result = BVweb.UserPasswordFormat.Hashed
                Case "encrypted", "e", "crypto"
                    result = BVweb.UserPasswordFormat.Encrypted
            End Select
            Return result
        End Function

    End Class

End Namespace